/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projectmangement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author trungquy
 */
public abstract class DAO {

    private Connection mysql_connection;
    private ResultSet mysql_result;
    private String mysql_connectionURL;
    private String mysql_driver;
    
    public boolean init(){
        return connect("localhost:3306/assignment", "root","");        
    }

    public boolean connect(String server, String username, String password) {
        try {
            //Load MySQL JDBC Driver
            mysql_driver = "com.mysql.jdbc.Driver";
            Class.forName(mysql_driver);

            //Open Connection
            mysql_connectionURL = "jdbc:mysql://" + server;
            mysql_connection = DriverManager.getConnection(mysql_connectionURL, username, password);
            return true;
        } catch (Exception x) {
            x.printStackTrace();
            return false;
        }
    }

    public boolean close() {
        try {
            mysql_connection.close();
            return true;
        } catch (Exception x) {
            x.printStackTrace();
            return false;
        }
    }

    public ResultSet query(String query) {
        //Create Stament object
        Statement stmt;

        /*
         * We want to keep things simple, so...
         *
         * Detect whether this is an INSERT, DELETE, or UPDATE statement      
         * And use the executeUpdate() function
         *
         * Or...
         * 
         * Detect whether this is a SELECT statment and use the executeQuery()
         * Function. 
         * 
         */

        if (query.toLowerCase().startsWith("select")) {
            //Use the "executeQuery" function becuase we have to retrive data
            //Return the data as a resultset
            try {
                //Execute Query
                stmt = mysql_connection.createStatement();
                mysql_result = stmt.executeQuery(query);
            } catch (Exception x) {
                x.printStackTrace();
            }

            //Return Result
            return mysql_result;
        } else {
            //It's an UPDATE, INSERT, or DELETE statement
            //Use the"executeUpdaye" function and return a null result
            try {
                //Execute Query
                stmt = mysql_connection.createStatement();
                stmt.executeUpdate(query);
            } catch (Exception x) {
                x.printStackTrace();
            }

            //Return nothing
            return null;
        }
    }
    @Override
    public void finalize(){
        try {
            super.finalize();
        } catch (Throwable ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            mysql_connection.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
